﻿using Pdf2epub.Interfaces;
using System.Windows;
using Xceed.Wpf.Toolkit;

namespace Pdf2epub.BusinessLogic
{
    internal class FeedbackService : CapsulingServiceBase, IFeedbackService
    {
        private readonly FeedbackServiceBusyContent m_BusyContent;
        private readonly BusyIndicator m_BusyIndicator;

        public FeedbackService(BusyIndicator busyIndicator)
        {
            m_BusyIndicator = busyIndicator;
            m_BusyContent = new FeedbackServiceBusyContent();
            m_BusyIndicator.BusyContent = m_BusyContent;
        }

        public void SetBusyText(string txt)
        {
            Application.Current.Dispatcher.Invoke(() => m_BusyContent.MainText = txt);
        }

        public void SetDetailText(string txt)
        {
            Application.Current.Dispatcher.Invoke(() => m_BusyContent.DetailText = txt);
        }

        protected override void CapsuleActivity()
        {
            SetBusy();
        }

        protected override void UnCapsuleActivity()
        {
            ReleaseBusy();
        }

        private void ReleaseBusy()
        {
            Application.Current.Dispatcher.Invoke(() =>
                                                      {
                                                          m_BusyIndicator.IsBusy = false;
                                                          m_BusyContent.MainText = string.Empty;
                                                          m_BusyContent.DetailText = string.Empty;
                                                      });
        }

        private void SetBusy()
        {
            Application.Current.Dispatcher.Invoke(() =>
                                                      {
                                                          m_BusyIndicator.IsBusy = true;
                                                          m_BusyContent.MainText = string.Empty;
                                                          m_BusyContent.DetailText = string.Empty;
                                                      });
        }
    }
}